package com.infoland.afanmall.base.view.viewPageCards1;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.SizeUtils;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.util.LogKit;

import java.util.ArrayList;
import java.util.List;

public class CoverFlowViewPager extends RelativeLayout implements OnPageSelectListener {

    /**
     * 适配器
     */
    private CoverFlowAdapter mAdapter;

    /**
     * 用于左右滚动
     */
    private ViewPager mViewPager;

    /**
     * 需要显示的视图集合
     */
    private List<View> mViewList;

    private OnPageSelectListener listener;

    public CoverFlowViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflate(context, R.layout.widget_cover_flow, this);
        mViewPager = findViewById(R.id.vp_conver_flow);
//        365dp 380
        int marginWidth = DensityUtils.dip2px(getContext(), 40);
        int displayWidth = DensityUtils.getDisplayWidth(getContext());
        int height = DensityUtils.dip2px(getContext(), 242);
        LayoutParams layoutParams = new LayoutParams(displayWidth - marginWidth, height);
        //        设置阴影展示所需要的边距
        int left_right = SizeUtils.dp2px(10);
        int top = SizeUtils.dp2px(7);
        int bottom = SizeUtils.dp2px(7);
        layoutParams.setMargins(left_right, top, left_right, bottom);
        //        layoutParams.(0,DensityUtils.dip2px(getContext(), 10),0,DensityUtils.dip2px(getContext(), 10));
        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        mViewPager.setLayoutParams(layoutParams);
        init();
    }

    /**
     * 初始化方法
     */
    private void init() {
        mViewList = new ArrayList<>();
        mAdapter = new CoverFlowAdapter(mViewList, getContext());   // 构造适配器，传入数据源
        mAdapter.setOnPageSelectListener(this); // 设置选中的回调
        mViewPager.setAdapter(mAdapter);
        mViewPager.addOnPageChangeListener(mAdapter);
        mViewPager.setOffscreenPageLimit(5);        // 预加载个数
        // 设置触摸事件的分发
        setOnTouchListener((v, event) -> {
            return mViewPager.dispatchTouchEvent(event);
        });

    }

    /**
     * 设置显示的数据，进行一层封装
     *
     * @param lists
     */
    public void setViewList(List<View> lists) {
        if (lists == null) {
            return;
        }
        mViewList = new ArrayList<>();
        for (View view : lists) {
            FrameLayout layout = new FrameLayout(getContext());
            // 设置padding 值，默认缩小
            layout.setPadding(CoverFlowAdapter.sWidthPadding, CoverFlowAdapter.sHeightPadding,
                    CoverFlowAdapter.sWidthPadding, CoverFlowAdapter.sHeightPadding);
            layout.addView(view);
            mViewList.add(layout);
        }
        mAdapter.setmViewList(mViewList);
        // 刷新数据
        mAdapter.notifyDataSetChanged();
    }



    /**
     * 当将某一个作为最中央时的回调
     *
     * @param listener
     */
    public void setOnPageSelectListener(OnPageSelectListener listener) {
        this.listener = listener;
    }


    // 显示的回调
    @Override
    public void select(int position) {
        if (listener != null) {
            listener.select(position);
        }
    }


}
